package structs

import (
	"github.com/shopspring/decimal"
)

type TrustStatus int

const (
	TRUST_STATUS_NOT       TrustStatus = 0 // 未交易
	TRUST_STATUS_PARTIALLY TrustStatus = 1 // 部分完成
	TRUST_STATUS_ALL       TrustStatus = 2 // 全部撮单完成
)

type UserCoinBalance struct {
	Id 				 int 		`json:"id"`
	Balance          string		`json:"balance"`
	BalanceAvailable string		`json:"balance_available"`
	BalanceLock      string		`json:"balance_lock"`
}

type Account struct {
	FromOver decimal.Decimal		`json:"from_over"`
	FromLock decimal.Decimal		`json:"from_lock"`
	ToOver   decimal.Decimal		`json:"to_over"`
	ToLock   decimal.Decimal		`json:"to_lock"`
}

// 每一笔委托单
type OneTrust struct {
	TrustId         int				`json:"trust_id"`
	Uid             int				`json:"uid"`
	TrustDealStatus TrustStatus 	`json:"trust_deal_status"`	// 交易状态
	DealNumber 		decimal.Decimal `json:"deal_number"`		// 交易数量，只需要记录被动用户即可

	OriginalTrustCoin *TrustCoin 	`json:"original_trust_coin"` 	// 原数据结构，便于对比
	TrustCoin         *TrustCoin	`json:"trust_coin"`
}

type OrderLogs struct {
	DealNumber decimal.Decimal 		`json:"deal_number"` 	// 交易数量
	DealPrice  decimal.Decimal 		`json:"deal_price"` 	// 交易价格
	DealTotal  decimal.Decimal		`json:"deal_total"` 	// n * p
	BuyUid     int					`json:"buy_uid"`
	SaleUid    int					`json:"sale_uid"`

	// 只需要被动单数据
	BuyerFromOver decimal.Decimal	`json:"buyer_from_over"`
	BuyerFromLock decimal.Decimal	`json:"buyer_from_lock"`
	BuyerToOver   decimal.Decimal	`json:"buyer_to_over"`
	BuyerToLock   decimal.Decimal	`json:"buyer_to_lock"`

	SellerFromOver decimal.Decimal	`json:"seller_from_over"`
	SellerFromLock decimal.Decimal	`json:"seller_from_lock"`
	SellerToOver   decimal.Decimal	`json:"seller_to_over"`
	SellerToLock   decimal.Decimal	`json:"seller_to_lock"`

	BuyerFee  decimal.Decimal		`json:"buyer_fee"`
	SellerFee decimal.Decimal		`json:"seller_fee"`

	Order    *OrderCoin				`json:"order"`
	Finances []*OrderCoinFinance	`json:"finances"`
}

type OriginAndNewBalance struct {
	OriginalUserCoin 			map[string]*UserCoinBalance			`json:"original_user_coin"`
	NewUserCoin 				map[string]*UserCoinBalance			`json:"new_user_coin"`
}

type Results struct {
	IsDealed            bool 									`json:"is_dealed"`					// 是否交易过
	AddActiveTrustToSet bool 									`json:"add_active_trust_to_set"`	// 是否将用户添加到SET待匹配列表
	RedisOptSuccess     bool									`json:"redis_opt_success"` 			// Redis操作是否成功
	DbOptSuccess        bool 									`json:"db_opt_success"` 			// 数据库操作是否成功
	CoinFrom            string									`json:"coin_from"`
	CoinTo              string									`json:"coin_to"`
	ActiveTrustType     TaskType        						`json:"active_trust_type"`			// 主动单类型，主动单是买单还是卖单
	ActiveTrust         *OneTrust        						`json:"active_trust"`				// 主动用户委托单
	PassiveTrusts       []*OneTrust      						`json:"passive_trusts"`				// 被动用户委托单
	ActiveAccount       *Account         						`json:"active_account"`				// 主动用户账户
	PassiveAccounts     map[int]*Account 						`json:"passive_accounts"`			// 被动用户账户
	OrderLogs           []*OrderLogs							`json:"order_logs"`
	UserAssetLogs       []*UserAssetLog					`json:"user_asset_logs"`
	TotalFeeNumber      map[string]decimal.Decimal 				`json:"total_fee_number"`			// 统计税总和，KEY为币种
	UserCoin 			map[int]*OriginAndNewBalance 			`json:"user_coin"`
	OfficialCoin 		*OriginAndNewBalance 					`json:"official_coin"`
}


func NewResults(from, to string, activeType TaskType) *Results {
	res := &Results{
		CoinFrom:        from,
		CoinTo:          to,
		ActiveTrustType: activeType,
		ActiveTrust:     new(OneTrust),
		PassiveTrusts:   make([]*OneTrust, 0),
		ActiveAccount:   new(Account),
		PassiveAccounts: make(map[int]*Account),
		OrderLogs:       make([]*OrderLogs, 0),
		UserAssetLogs:   make([]*UserAssetLog, 0),
		TotalFeeNumber:  make(map[string]decimal.Decimal),
		UserCoin: 		 make(map[int]*OriginAndNewBalance, 0),
		OfficialCoin: 	 new(OriginAndNewBalance),
	}

	return res
}
